<?php

namespace App;
use App\model\UserWechat;
use Illuminate\Database\Eloquent\Builder;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\User
 *
 * @property int $id
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string|null $remember_token
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
 * @property-read \App\UserInfo $userinfo
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User permission($permissions)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User role($roles, $guard = null)
 * @property string|null $mobile 手机号
 * @property int|null $project_id 项目
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereProjectId($value)
 * @property string|null $address 地址
 * @property-read mixed $group_name
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAddress($value)
 * @property string|null $avatar
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAvatar($value)
 * @property int|null $is_passed 是否审核通过
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereIsPassed($value)
 * @property string|null $session_key
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereSessionKey($value)
 * @property string|null $unionid
 * @property-read \App\model\UserWechat $userWechat
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUnionid($value)
 * @property string $no_project_id


 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereNoProjectId($value)
 */
class User extends Authenticatable implements  JWTSubject
{
    use Notifiable,HasRoles;
    protected $guard_name = 'web';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $appends=[
        'groupName',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
   public function userinfo(){
        return $this->hasOne(UserInfo::class,'user_id');
   }
   public function userWechat(){
       return $this->hasOne(UserWechat::class,'unionid','unionid');
   }
   public function getGroupNameAttribute($value)
   {
       if(preg_match ("/^[A-Za-z]/", $this->attributes['name'])){
            return str_limit($this->attributes['name'],1,'');
       }else{
         return  str_limit(pinyin_abbr($this->attributes['name'],'1'),1,'');
       }
   }
    public function getProjectIdAttribute($value)
    {
        return explode(',', $value);
    }
    public function getNoProjectIdAttribute($value)
    {
        return explode(',', $value);
    }
    public function setProjectIdAttribute($value)
    {
        $this->attributes['project_id'] = implode(',', $value);
    }
    public function setNoProjectIdAttribute($value)
    {
        $this->attributes['no_project_id'] = implode(',', $value);
    }
}
